#!/usr/bin/env python
import os
import sys
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
if __name__ == '__main__':
    ep = ExecutePreprocessor(timeout=60, kernel_name='python3')
    for cwd, dirs, fnames in os.walk(sys.argv[1]):
        for fname in sorted(fnames):
            if fname.endswith('.ipynb'):
                with open(os.path.join(cwd, fname)) as f:
                    nb = nbformat.read(f, as_version=4)
                try:
                    ep.preprocess(nb, {'metadata': {'path': cwd}})
                except Exception as exc:
                    print(fname, '-->', exc.evalue)
                else:
                    print(fname, '--> OK')
